package com.smartcruise.util;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

import com.smartcruise.R;

/**
 * Created by FengChaoQun
 * on 2017/1/16
 */

public class FragmentUtils {

    public static void replace(FragmentManager fragmentManager, int container, Fragment fragment, boolean addToBacStack) {
        if (addToBacStack) {
            fragmentManager.beginTransaction()
                    .setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_right,
                            R.anim.slide_in_left, R.anim.slide_out_left)
                    .replace(container, fragment, fragment.getClass().getSimpleName())
                    .addToBackStack(fragment.getClass().getSimpleName())
                    .commit();
        } else {
            fragmentManager.beginTransaction()
                    .setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_right,
                            R.anim.slide_in_left, R.anim.slide_out_left)
                    .replace(container, fragment, fragment.getClass().getSimpleName())
                    .commit();
        }
    }

    public static void replace(FragmentManager fragmentManager, int container, Fragment fragment) {
        replace(fragmentManager, container, fragment, true);
    }

    public static void add(FragmentManager fragmentManager, int container, Fragment fragment) {
        fragmentManager.beginTransaction()
                .add(container, fragment, fragment.getClass().getSimpleName())
                .commit();
    }

    public static Fragment find(FragmentManager fragmentManager, Class cl) {
        return fragmentManager.findFragmentByTag(cl.getSimpleName());
    }

    public static void popBack(FragmentManager fragmentManager, Class<? extends Fragment> fragmentClass) {
        fragmentManager.popBackStackImmediate(fragmentClass.getSimpleName(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
    }
}
